package com.fly.video.downloader;

import com.fly.video.downloader.core.Validator;
import com.fly.video.downloader.slice.VideoSearchAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import org.apache.commons.lang3.StringUtils;

public class VideoSearchAbility extends Ability {
    protected TextField editor;
    protected String editorText;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        super.setMainRoute(VideoSearchAbilitySlice.class.getName());
        super.setUIContent(ResourceTable.Layout_fragment_video_search);
        editor= (TextField) findComponentById(ResourceTable.Id_video_search_editor);
        findComponentById(ResourceTable.Id_video_search_btn_clear).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                editor.setText("");
            }
        });
        findComponentById(ResourceTable.Id_video_search_btn_enter).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                btn_enter();
            }
        });
        editorText=intent.getStringParam("lastClip");
        if (null != editor&& !StringUtils.isEmpty(editorText)){
            editor.setText(editorText);
        }
    }
    protected void btn_enter(){
        String str = editor.getText().toString();
        if (str.isEmpty() || !Validator.containsUrl(str))
        {
            getUITaskDispatcher().asyncDispatch(new Runnable() {
                @Override
                public void run() {
                    new ToastDialog(getContext()).setContentText(getString(ResourceTable.String_noURL)).show();
                }
            });
        } else {
            editor.setText("");
            Intent intent=new Intent();
            intent.setParam("str",str);
            setResult(0,intent);
            terminateAbility();
        }
    }
}
